<template>
  <div class="add-admin">
    <h1>添加管理员</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="账户名" label-width="100px">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" label-width="100px">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="权限" label-width="100px">
        <el-select v-model="form.region" placeholder="请选择管理员权限">
          <el-option label="超级管理员" value="superAdmin"></el-option>
          <el-option label="普通管理员" value="admin"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="onReset" round>重置</el-button>
        <el-button type="primary" @click="onSubmit" round>提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Axios from "axios";
export default {
  data() {
    return {
      form: {
        username: "",
        region: "",
        password: ""
      }
    };
  },
  methods: {
    async onSubmit() {
      const { username, region, password } = this.form;
      if (username != "") {
        if (password != "") {
          if (region != "") {
            const isSuccess = await this.addAdmin();
            if (isSuccess) {
              this.$message({ message: "添加成功", type: "success" });
            } else {
              this.$message.error("添加失败");
            }
            this.form = {
              username: "",
              region: "",
              password: ""
            };
          } else {
            this.$message("请选择管理权限");
          }
        } else {
          this.$message("请输入密码");
        }
      } else {
        this.$message("请输入账号名");
      }
    },
    onReset() {
      this.form = {
        username: "",
        region: "",
        password: ""
      };
    },
    //请求添加管理员
    async addAdmin() {
      const res = await Axios.post("/admin/addAdmin", {
        username: this.form.username,
        password: this.form.password,
        identity: this.form.region
      });
      return res.data.success;
    }
  }
};
</script>

<style lang="less">
.add-admin {
  margin-left: 120px;
  width: 360px;
  h1 {
    margin-bottom: 30px;
    font-size: 26px;
  }
}
</style>